<?php
/**
 * Модель product
 * 
 * Класс модели
 * @author Anton Zavidov
 * @package myLiveShop
 */

/**
 * Модель product
 *
 */
class ModelProduct extends Model {
	
	const TABLE				= 'product';
	const F_PRODUCT_ID  	= 'product_id';
	const F_SHOP_ID  	= 'shop_id';
	const F_CATEGORY_ID  	= 'category_id';
	const F_IMAGE_ID  	= 'image_id';
	const F_CREATED  	= 'created';
	const F_PUBLISH  	= 'publish';
	const F_DELETED  	= 'deleted';
	const F_SORT  	= 'sort';
	const F_DESCRIPTION  	= 'description';
	const F_TITLE  	= 'title';
	const F_USER_ID  	= 'user_id';
	const F_LANG_ID  	= 'lang_id';
	const F_PRICE  	= 'price';
	const F_CURRENCY_ID  	= 'currency_id';
	const F_REAL_PRICE  	= 'real_price';
	const F_RAITING_SUM  	= 'raiting_sum';
	const F_RAITING_COUNT  	= 'raiting_count';
			
	public function __construct($className=null){
		parent::__construct(__CLASS__);
		$this->name='product';
		$this->id_field=array(self::F_PRODUCT_ID);
		$this->fields=array(
		self::TABLE.DOT.self::F_PRODUCT_ID=> new Field($this,self::F_PRODUCT_ID,self::TABLE,array(
				'auto'=>true,
				'important'=>true,
				'lenght'=>11)),
		self::TABLE.DOT.self::F_SHOP_ID=> new Field($this,self::F_SHOP_ID,self::TABLE,array(
				'important'=>true,
				'lenght'=>9,
				'join_table'=>'shop')),
		self::TABLE.DOT.self::F_CATEGORY_ID=> new Field($this,self::F_CATEGORY_ID,self::TABLE,array(
				'lenght'=>9,
				'join_table'=>'category')),
		self::TABLE.DOT.self::F_IMAGE_ID=> new Field($this,self::F_IMAGE_ID,self::TABLE,array(
				'lenght'=>11,
				'join_table'=>'image')),
		self::TABLE.DOT.self::F_CREATED=> new Field($this,self::F_CREATED,self::TABLE,array(
				'important'=>true,
				'auto'=>true,
				'type'=>Field::TYPE_DATETIME)),
		self::TABLE.DOT.self::F_PUBLISH=> new Field($this,self::F_PUBLISH,self::TABLE,array(
				'important'=>true,
				'lenght'=>1,
				'type'=>Field::TYPE_STRING,
				'validate_type'=>Validator::VALIDATE_HASH)),
		self::TABLE.DOT.self::F_DELETED=> new Field($this,self::F_DELETED,self::TABLE,array(
				'important'=>true,
				'lenght'=>1,
				'validate_type'=>Validator::VALIDATE_BOOL)),
		self::TABLE.DOT.self::F_SORT=> new Field($this,self::F_SORT,self::TABLE,array(
				'important'=>true,
				'lenght'=>5)),
		self::TABLE.DOT.self::F_DESCRIPTION=> new Field($this,self::F_DESCRIPTION,self::TABLE,array(
				'type'=>Field::TYPE_TEXT)),
		self::TABLE.DOT.self::F_TITLE=> new Field($this,self::F_TITLE,self::TABLE,array(
				'lenght'=>255,
				'type'=>Field::TYPE_STRING)),
		self::TABLE.DOT.self::F_USER_ID=> new Field($this,self::F_USER_ID,self::TABLE,array(
				'lenght'=>11,
				'join_table'=>'user')),
		self::TABLE.DOT.self::F_LANG_ID=> new Field($this,self::F_LANG_ID,self::TABLE,array(
				'auto'=>true,
				'lenght'=>2,
				'join_table'=>'lang')),
		self::TABLE.DOT.self::F_PRICE=> new Field($this,self::F_PRICE,self::TABLE,array(
				'lenght'=>6)),
		self::TABLE.DOT.self::F_CURRENCY_ID=> new Field($this,self::F_CURRENCY_ID,self::TABLE,array(
				'lenght'=>2,
				'join_table'=>'currency')),
		self::TABLE.DOT.self::F_REAL_PRICE=> new Field($this,self::F_REAL_PRICE,self::TABLE,array(
				'lenght'=>6)),
		self::TABLE.DOT.self::F_RAITING_SUM=> new Field($this,self::F_RAITING_SUM,self::TABLE,array(
				'lenght'=>6)),
		self::TABLE.DOT.self::F_RAITING_COUNT=> new Field($this,self::F_RAITING_COUNT,self::TABLE,array(
				'lenght'=>5))		);
	}
	
	/**
	 * Меняем рейтинг товара
	 *
	 * @param int $id ID товара
	 * @param int $raiting оченка рейтинга
	 * @return resource
	 */
	public function setProductRaiting($id,$raiting){
		return $this->db->query('CALL SetProductRaiting('.$id.', '.$raiting.')');
	}

	public function getAllTags($product_id){
		import("model.tag");
		$tag_to_product = import("model.tag_to_product",true);
		return $tag_to_product->load(null,array(ModelTag::F_TAG_ID, ModelTag::TABLE.DOT.ModelTag::F_TITLE),array(
			'where'=>ModelTag_to_product::F_PRODUCT_ID .'='.$product_id,
			'join'=>array(
					array('table'=>ModelTag::TABLE, 'key'=>ModelTag::F_TAG_ID, 'join_table'=>ModelTag_to_product::TABLE, 'join_field'=>ModelTag_to_product::F_TAG_ID)
			)
		));
	}
}
